package com.zhou.blog.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhou.blog.dao.mapper.ArticleMapper;
import com.zhou.blog.dao.pojo.Article;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * @title:ThreadService
 * @Author Zhou
 * @Date:2022/5/4 11:42
 * @Version 1.0
 */
@Service
@Component
public class ThreadService {

    //将此操作放入线程池执行
    @Async("taskExecutor")
    public void updateViewCount(ArticleMapper articleMapper, Article article) {
        int viewCounts = article.getViewCounts();
        Article articleUpdate = new Article();
        articleUpdate.setViewCounts(viewCounts + 1);
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getId,article.getId());
        //判断数据有没有被其他线程修改
        queryWrapper.eq(Article::getViewCounts,viewCounts);
        articleMapper.update(articleUpdate,queryWrapper);



    }
}
